package com.sansecy.customview;

import android.graphics.Paint;

public class PaintUtil {
    public static int getTextHeight(int baseLineY, Paint paint) {
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
//        int top = baseLineY + fm.top;
//        int bottom = baseLineY + fm.bottom;
//        return bottom - top;
        return fm.descent - fm.ascent;
    }

    public static int getTextWidth(String text, Paint paint) {
        return (int) paint.measureText(text);
    }

    /**
     * 返回基线的y坐标
     *
     * @param top   要绘制的Text最上角的top值
     * @param paint 要绘制的画笔
     * @return 基线的Y值
     */
    public static int getBaseLineY(int top, Paint paint) {
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
//        return top - fm.ascent;
        return Math.abs(fm.ascent);
    }

    public static int getNoPaddingBaseLineY(int top, Paint paint) {
        Paint.FontMetricsInt fm = paint.getFontMetricsInt();
//        return top - fm.ascent;
        return -fm.ascent;
    }

}
